<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.key" placeholder="套餐名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="queryData">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="50">
			</el-table-column>
			<el-table-column prop="mealName" label="套餐名称" width="150" sortable>
			</el-table-column>
      <el-table-column prop="mealPrice" label="套餐价格" width="150" sortable>
      </el-table-column>

      <el-table-column prop="status" label="套餐状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0" style="color: #11b95c">正常</span>
          <span v-else style="color: #ff4d51">禁用</span>
        </template>
      </el-table-column>

			<el-table-column label="操作" width="250">
				<template scope="scope">
					<el-button size="small" @click="handleSave(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="search.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total"
          style="float: right">
      </el-pagination>
		</el-col>
    
		<!--新增/修改界面-->
		<el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="saveForm" label-width="80px" ref="saveForm">
				<el-form-item label="套餐名称" prop="name">
					<el-input v-model="saveForm.mealName" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="套餐价格" prop="intro">
          <el-input v-model="saveForm.mealPrice" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>

	export default {
		data() {
			return {
				search: {
					key: '',
          pageSize:5,
          currentPage:1
				},
        pageInfo:{
          data: [],
          total: 0
        },
				listLoading: false,
				sels: [],//列表选中列

        saveFormVisible:false,
        saveForm:{
          id:null,
          mealName:"",
          mealPrice:"",
          status:""
        },

        reviewFormVisible:false,
        reviewForm:{
          region:"",
          desc: ''
        },
			}
		},
		methods: {
      //获取店铺列表
      getMeal() {
        this.$http.get("/sys/meal",this.search)
            .then(res => {
              res = res.data;
              if(res.success){
                console.log(res);
                this.pageInfo = res;
                this.listLoading = false;
              }
            })
            .catch(res => {});
      },
      //点击新增
      handleAdd(){
        this.saveFormVisible = true;
        this.saveForm = {
          id:null,
          name:"",
          tel:"",
          address:"",
          logo:""
        };
      },
      //当每页条数发生变化
      handleSizeChange(val) {
        this.search.pageSize = val;
        this.getMeal();
      },
      //当前页发生变化
			handleCurrentChange(val) {
				this.search.currentPage = val;
				this.getMeal();
			},
      //点击查询
      queryData() {
        this.search.currentPage = 1;
        this.getMeal();
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading = true;
          this.$http.delete("/sys/meal/"+row.id)
              .then(res => {
                res = res.data;
                if(res.success){
                  this.$message({
                    type:"success",
                    message:"删除成功"
                  })
                }else {
                  this.$message.error("删除失败"+res.msg)
                }
                this.listLoading = false;
                this.queryData();
              })
              .catch(res => {});
				}).catch(() => {});
			},
			//显示新增/修改界面
			handleSave: function (index,row) {
				this.saveFormVisible = true;
        this.saveForm = row;
        console.log(row);
      },
			//新增/修改
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {})
                .then(() => {
                  //NProgress.start();
                  let para = Object.assign({}, this.saveForm);
                  this.$http.put("/sys/meal",para)
                      .then(res => {
                        this.saveFormVisible = false;
                        this.$message({
                          type:"success",
                          message:"保存成功"
                        })
                        this.queryData();
                      })
                      .catch(res => {});
						});
					}
				});
			},
      //列表选中列
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.patch("/sys/meal",ids)
              .then(res => {
                res = res.data;
                console.log(res);
                if(res.success){
                  this.$message({
                    type:"success",
                    message:"删除成功"
                  })
                }else {
                  this.$message.error("删除失败"+res.msg)
                }
                this.listLoading = false;
                this.queryData();
              })
              .catch(res => {});
				}).catch(() => {});
			}
		},
		mounted() {
			this.getMeal();
		}
	}

</script>

<style scoped>

</style>